﻿using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{

	public partial class BodyMeaMgrImp
	{

		//For Waist;
		//public KDXIMILineAnalysis mWaistLineAnalysis;
//		public ClusterSmoothTool waistSmthWideness;
//		//public ClusterSmoothTool waistSmthThickness;
//		public FloatSmoothTool waistSmthThickness;

		public ArrayList waistSmthWidthArr=new ArrayList();
		public ArrayList waistSmthThickArr=new ArrayList();



		float waistThicknessRatio = 0.94f;
		float waistWidenessRatio = 0.94f;
		float waistRatio = 0.98f;  //the value detected by knt is a bit too large. 

		public float waistWideness = 0;  //We set the distance between shoulders *chestWidShoulderWidthRatio as the wideness of chest.
		public float waistThickness = 0f;

		float waistWidShoulderWidthRatio = 1f;



		//because the depth valuse in side posture is on the surface.
		                         //Which seems a bit too close to the device as the depth of waist. So we 
		                          // add a adjust value,  adjustDeepingLengthForSideWaist, now the value is 80mm.


	

		   public float tmpWid;



		public void ProcessWaistSide(ref Dictionary<int, ImiPlayerInfo> playerInfos ,int userID, bool isDrawOnDepthMap)
		{


			//method after 11 21

			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;


			int NeckPixPosX=0;
			int NeckPixPosY=0;
			int NeckPixPosZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref NeckPixPosX, ref NeckPixPosY, ref NeckPixPosZ, 
			                      ImiSkeleton.Index.SHOULDER_CENTER,isDrawOnDepthMap);


			int SpinePixPosX=0;
			int SpinePixPosY=0;
			int SpinePixPosZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref SpinePixPosX, ref SpinePixPosY, ref SpinePixPosZ, 
			                      ImiSkeleton.Index.SPINE,isDrawOnDepthMap);

			
			int hipsMidPixPosX=0;
			int hipsMidPixPosY=0;
			int hipsMidPixPosZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref hipsMidPixPosX, ref hipsMidPixPosY, ref hipsMidPixPosZ, 
			                      ImiSkeleton.Index.HIP_CENTER,isDrawOnDepthMap);
			
			int hipleftX=0;
			int hipleftY=0;
			int hipleftZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref hipleftX, ref hipleftY, ref hipleftZ, 
			                      ImiSkeleton.Index.HIP_LEFT,isDrawOnDepthMap);






			
			//To measure the hips thickness.
			int hipssideupx=RangeValue(hipsMidPixPosX, 0 ,dw);;
			//int hipssideupy=RangeValue((hipsMidPixPosY+((hipsMidPixPosY-hipleftY)/2)),0 ,dh); 
			int hipssideupy=RangeValue(WaistY-(int)(((float)(WaistY-NeckPixPosY))*0.14f),
			                           0 ,dh); 
			

			int hipssidedownx=RangeValue(hipsMidPixPosX, 0 ,dw);
			//int hipssidedowny=RangeValue(hipsMidPixPosY-(hipsMidPixPosY-hipleftY)/2, 0 ,dh);
			int hipssidedowny=RangeValue(WaistY+(int)(((float)(WaistY-NeckPixPosY))*0.2f),
			                             0 ,dh); 
			if(hipssideupy==hipssidedowny)
			{
				hipssideupy=hipssidedowny+1;
			}
						
			Vector2 HipsUpPixPos = new Vector2(hipssideupx,hipssideupy );
			Vector2 HipsDownPixPos = new Vector2(hipssidedownx,hipssidedowny );
						
			//we add a adjusting deeping value on the waist surface depth.
			
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [hipssidedowny *dw +hipssidedownx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [hipssideupy *dw +hipssideupx] & 7;
			
			
			//Only when the key pix pos symplify an player pixel.
			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
				
				float WaistSideTmp=GetThicknessFromPixPosesArr(dw,dh,depthPlayerDataOriginal,
				                                             boundThreshval,HipsUpPixPos,HipsDownPixPos,adjustDeepingLengthForSideWaist, isDrawOnDepthMap);	
				
				
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
				//					hipsSmthThickness.InsertValue (hipsSideTmp);
				//					hipsThickness=hipsSmthThickness.GetFilteredValue();


				if(WaistSideTmp>0.1f)
				{
				    waistSmthThickArr.Add(WaistSideTmp);
				}
			}
		}

		//In 2.17 06.  For now we use following func locate the hori chest line.
		//We use hips center as the position of waist.
		public void ProcessWaist2(ref Dictionary<int, ImiPlayerInfo> playerInfos ,	int userID, bool isDrawOrNot )
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;
			
			
			
			int SpineX=0;
			int SpineY=0;
			int SpineZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref SpineX, ref SpineY, ref SpineZ, 
			                      ImiSkeleton.Index.SPINE,isDrawOrNot);
			
			
			SpineX=RangeValue(SpineX, 0 ,dw);;
			SpineY=RangeValue(SpineY, 0 ,dh); 
			
			int tempPlayerIndex0 = depthPlayerDataOriginal  [SpineY *dw +SpineX] & 7;
			
			
			int neckX=0;
			int neckY=0;
			int neckZ=0;
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref neckX, ref neckY, ref neckZ, 
			                      ImiSkeleton.Index.SHOULDER_CENTER,isDrawOrNot);
			
			
			neckX=RangeValue(neckX, 0 ,dw);;
			neckY=RangeValue(neckY, 0 ,dh); 
			
			
			
			
			WaistY=SpineY+(int)(((float)(SpineY-neckY))*0.6f);
			
			//Only when the key pix pos symplify an player pixel.
			if(tempPlayerIndex0>0)
			{
				
				if( isDrawOrNot )
				{
					
					for(int i=MinContourLimitX;i<MaxContourLimitX;i++)
					{
						DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
						                                                  i,WaistY,
						                                                  3,allUsersDepthData,0,255,0,255);
					}
					
					
				}
			}


			waistFrontDepthLineMgr.ClearGraph ();
			waistFrontDepthLineMgr.ClearSampleDatas ();
			waistFrontDepthLineMgr.enableDrawing = true;
			waistFrontDepthLineMgr.FillRowNormaliezedDepthDataAndFillZeroGap (DepthPlayerDataOriginal, MinContourLimitX, MaxContourLimitX, WaistY, 640, 480);
//			waistFrontDepthLineMgr.DrawDepthAnalysisArr ();
//			waistFrontDepthLineMgr.DrawGraph ();
			LeftWaistLimit = MinContourLimitX + waistFrontDepthLineMgr.sharpBeginIdx + waistFrontDepthLineMgr.armBodyGapLeftIdx;
			RightWaistLimit = MinContourLimitX + waistFrontDepthLineMgr.sharpBeginIdx + waistFrontDepthLineMgr.armBodyGapRightIdx;
			
		


		}
		
		public float GetWaistGirth2()
		{
			return IMIBodyMeaTool.GetGirth2(waistFrontDepthLineMgr.frontLen0, waistFrontDepthLineMgr.frontLen1,
			                 waistFrontDepthLineMgr.frontSideChordThick,waistThickness,remainGirthRatioWaist0,remainGirthRatioWaist1);			
		}

		public float  GetWaistGirth( )
		{
			return IMIBodyMeaTool.CalculateWaistGirth(waistWideness, waistThickness,waistRatio);
			
		}

		public float WaistfrontArcLength()
		{
			return waistFrontDepthLineMgr.frontLen0;
		}
		
		public float WaistfrontChordLength()
		{
			return waistFrontDepthLineMgr.frontLen1;
		}





	}
}
